package co.gov.mintic.Biblioteca.activitys;

import java.util.List;

import co.gov.mintic.Biblioteca.android.DatosLibro;
import co.gov.mintic.Biblioteca.beans.Registro;
import co.gov.mintic.Biblioteca.constants.Constants;
import co.gov.mintic.Biblioteca.dataSource.Datasource;
import co.gov.mintic.Biblioteca.android.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public abstract class AbstractListViewActivity extends ListActivity {	

	protected Datasource datasource;
	protected static final int PAGESIZE = 5;
	protected TextView textViewDisplaying;
	protected View footerView;
	protected boolean loading = false;

	protected class LoadNextPage extends AsyncTask<String, Void, String> {
		private List<Registro> newData = null;

		@Override
		protected String doInBackground(String... arg0) {
			// para que de tiempo a ver el footer ;)
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				Log.e("AbstractListActivity", e.getMessage());
			}
			newData = datasource.getData(getListAdapter().getCount(), PAGESIZE);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			CustomArrayAdapter customArrayAdapter = ((CustomArrayAdapter) getListAdapter());
			for (Registro value : newData) {
				customArrayAdapter.add(value);
			}
			customArrayAdapter.notifyDataSetChanged();

			getListView().removeFooterView(footerView);
			updateDisplayingTextView();
			loading = false;
		}

	}

	protected void updateDisplayingTextView() {
		textViewDisplaying = (TextView) findViewById(R.id.displaying);
		String text = getString(R.string.display);
		text = String.format(text, getListAdapter().getCount(),
				datasource.getSize());
		textViewDisplaying.setText(text);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		Registro registro = (Registro) getListAdapter().getItem(position);
		
		Intent hackbookIntent = new Intent().setClass(this, DatosLibro.class);

		hackbookIntent.putExtra("Titulo", registro.getTitulo());
		hackbookIntent.putExtra("Autor", registro.getAutor());
		hackbookIntent.putExtra("Tema", registro.getTema());
		hackbookIntent.putExtra("Descripcion", registro.getDescripcion());
		hackbookIntent.putExtra("Recursos", getSources(registro.getFuente()));
		hackbookIntent.putExtra("Formato", getFormat(getSources(registro.getFuente())));

		this.startActivity(hackbookIntent);

	}

	/**
	 * 
	 * @param firstVisibleItem
	 * @param visibleItemCount
	 * @param totalItemCount
	 * @return
	 */

	protected boolean load(int firstVisibleItem, int visibleItemCount,
			int totalItemCount) {
		boolean lastItem = firstVisibleItem + visibleItemCount == totalItemCount
				&& getListView().getChildAt(visibleItemCount - 1) != null
				&& getListView().getChildAt(visibleItemCount - 1).getBottom() <= getListView()
						.getHeight();
		boolean moreRows = getListAdapter().getCount() < datasource.getSize();
		return moreRows && lastItem && !loading;

	}

	/**
	 * 
	 * @param source
	 * @return
	 */
	public String getSources(String source) {
		String book = "";
				
		String[] sources = source.split(",");
		if (source.length() > 0)
			book = sources[0] != null ? sources[0].trim() : "";
			
		return book;
	}
	
	public static String getFormat(String url){
		String fuente = url;
		String formato ="";		
			
			if (fuente.toLowerCase().indexOf(Constants.FORMATOMP3) > 0) {
				formato = "mp3";
			} else if (fuente.toLowerCase().indexOf(Constants.FORMATOPDF) > 0) {
				formato = "pdf";
			} else {
				formato = "com";
			}				
			return formato;
	}
}
